// pages/my/saleData/saleData.js
import moment from '../../../utils/moment.js'
var app = getApp()
Page({
  data: {
    days_style:[],
    statementList:[],
    brandList:[],
    stockList:[],
    partss:[],
    vehicles:[],
    brandType:'',
    brand:'',
    brandId:'',
    brandPick:'',
    brandPickId:'',
    saleBrand:'',
    saleBrandId:'',
    vehicleStock:'',
    partsStock:'',
    timeType:'',
    stockIndex:0,
    allConfirm:false,
    calendarShow:false,
    year: new Date().getFullYear()+"",      // 年份
    month: new Date().getMonth() + 1<10?"0"+(new Date().getMonth()+1):new Date().getMonth() + 1+'',    // 月份
    day: new Date().getDate()+"",
    prev: false,
    next: false,
    pickerView:false,
  },
  onLoad: function (options) {
    app.getMemInfo().then(res=>{
      let accountId = res.data.id
      let agentId = res.data.agentId
      this.setData({
        accountId,
        agentId
      })
      this.getTime(accountId)
      this.getBrands(agentId)
    })
    this.dayChoose()
  },
  onShow: function () {

  },
  //点击日历事件
  dayClick(e) {
    console.log(e)
    this.setData({
      allConfirm:false,
      year:e.detail.year+'',
      month:e.detail.month<10?'0'+e.detail.month:''+e.detail.month,
      day:e.detail.day+''
    })
    this.dayChoose()
  },
  //日期选择
  dayChoose(){
    let days_style=[]
    days_style.push({
      month:'current',
      day:this.data.day,
      color:'#fff',
      background:'#539FFF'
    })
    this.setData({
      days_style
    })
  },
  //打开日历
  bindTimeCheck(e){
    let type = e.currentTarget.dataset.type
    this.setData({
      calendarShow:true,
      timeType:type
    })
  },
  //日历取消按钮事件
  bindCancel(e){
    this.setData({
      calendarShow:false
    })
  },
  //日历确定按钮事件
  bindConfirm(){
    this.setData({
      calendarShow:false
    })
    this.getTime(this.data.accountId)
  },
  //日历全部按钮事件
  allConfirm(e){
    let days_style = []
    this.setData({
      allConfirm:true,
      days_style
    })
  },
  next(e){
    console.log(e.detail.currentMonth)
    this.setData({
      year:e.detail.currentYear+'',
      month:e.detail.currentMonth<10?'0'+e.detail.currentMonth:''+e.detail.currentMonth
    })
  },
  prev(e){
    this.setData({
      year:e.detail.currentYear+'',
      month:e.detail.currentMonth<10?'0'+e.detail.currentMonth:''+e.detail.currentMonth
    })
  },
  getTime(accountId){
    let startTime=''
    let endTime=''
    let data = {}
    if (this.data.allConfirm) {
      startTime =  this.data.year+'-'+this.data.month+'-01'
      endTime = moment(startTime).endOf('month').format('YYYY-MM-DD')
      data = {
        accountId,
        startTime,
        endTime,
        brandId:this.data.saleBrandId
      }
      if (this.data.timeType==0) {
        this.getCashierList(data)
      }else{
        this.getBrandStock(this.data.brandId,startTime,endTime)
      }
    }else{
      startTime = this.data.year+'-'+this.data.month+'-'+this.data.day
      endTime = startTime
      data = {
        accountId,
        startTime,
        endTime,
        brandId:this.data.saleBrandId
      }
      if (this.data.timeType==0) {
        this.getCashierList(data)
      }else{
        this.getBrandStock(this.data.brandId,startTime,endTime)
      }
    }
  },
  getCashierList(data){
    var that = this
    console.log(data)
    app.request('wx_app/agent/cashier',data,'GET').then(res=>{
      console.log(res)
      that.setData({
        statementList:res.data
      })
    })
  },
  //根据代理商ID获取下属品牌列表
  getBrands(agentId){
    let data = {
      agentId
    }
    app.request('wx_app/banner/getByAgentId',data,'GET').then(res=>{
      console.log(res)
      let brandList = []
      brandList.push({
        name:'全部',
        brand_id:''
      })
      res.data.forEach(item=>{
        brandList.push(item)
      })
      
      this.setData({
        brandList,
        brand:brandList[0].name,
        brandId:brandList[0].brand_id,
        brandPick:'全部',
        // brandPickId:brandList[0].brand_id,
        saleBrand:'全部',
        // saleBrandId:brandList[0].brand_id
      })
      this.getBrandStock(brandList[0].brand_id,'','')
    })
  },

  bindPickerChange(e) {
    console.log(e.detail.value)
    if (this.data.brandType=='1') {
      this.setData({
        brandPick:this.data.brandList[e.detail.value[0]].name,
        brandPickId:this.data.brandList[e.detail.value[0]].brand_id
      })
    }else{
      this.setData({
        saleBrand:this.data.brandList[e.detail.value[0]].name,
        saleBrandId:this.data.brandList[e.detail.value[0]].brand_id
      })
    }
  },
  closePicker(){
    this.setData({
      pickerView:false
    })
  },
  openPicker(e){
    this.setData({
      pickerView:true,
      brandType:e.currentTarget.dataset.type
    })
  },
  confirmPicker(){
    if (this.data.brandType==1) {
      this.setData({
        pickerView:false,
        brand:this.data.brandPick,
        brandId:this.data.brandPickId
      })
      this.getBrandStock(this.data.brandPickId,'','')
    }else{
      this.setData({
        pickerView:false
      })
      this.getTime(this.data.accountId)
    }
  },
  getBrandStock(brandId,startTime,endTime){
    let data = {
      agentId:this.data.agentId,
      brandId,
      startTime,
      endTime
    }
    console.log(data)
    // app.request('wx_app/agent/stock/inventoryData',data,'GET').then(res=>{
    //   console.log(res)
    //   let vehicles = []
    //   let partss = []
    //   res.data.vehicles.forEach(item=>{
    //     vehicles.push(item)
    //   })
    //   res.data.partss.forEach(item=>{
    //     partss.push(item)
    //   })
    //   this.setData({
    //     vehicleStock:res.data.vehicle.stock,
    //     partsStock:res.data.parts.stock,
    //     vehicles,
    //     partss
    //   })
    // })
    app.request('wx_app/user_order/salesStatistics',data,'GET').then(res=>{
      console.log(res)
      let [vehicles,partss] = [[],[]]
      res.data.vehicles.forEach(item=>{
        vehicles.push(item)
      })
      res.data.partss.forEach(item=>{
        partss.push(item)
      })
      this.setData({
        vehicleStock:res.data.vehicle.amount,
        partsStock:res.data.parts.amount,
        oldStock:res.data.oldSku.amount,
        vehicles,
        partss
      })
    })
  },
  getParts(e){
    let i = e.currentTarget.dataset.i
    this.setData({
      stockIndex:i
    })
  },
  getVehicles(e){
    let i = e.currentTarget.dataset.i
    this.setData({
      stockIndex:i
    })
  },
  getDetail(e){
    if (this.data.saleBrand=='全部') {    
      let id = e.currentTarget.dataset.id
      let price = e.currentTarget.dataset.price
      let startTime = ''
      let endTime = ''
      if (this.data.allConfirm) {
        startTime =  this.data.year+'-'+this.data.month+'-01'
        endTime = moment(startTime).endOf('month').format('YYYY-MM-DD')
      }else{
        startTime = this.data.year+'-'+this.data.month+'-'+this.data.day
        endTime = startTime
      }
      wx.navigateTo({
        url:'saleDataDetail?id='+id+'&price='+price+'&start='+startTime+'&end='+endTime
      })
    }
  }
})